<template>
  <div class="container">
    <!-- 导航固定定位 fixed -->
    <van-nav-bar fixed title="搜索结果" left-arrow @click-left="$router.back()" />
    <!-- 文章列表 -->
    <van-list v-model="loading" @load="onLoad" :finished="finished" finished-text="没有更多了">
      <van-cell-group>
        <van-cell
          :to="`/article/${item.art_id.toString()}`"
          :key="item.art_id.toString()"
          v-for="item in articles"
        >
          <div class="article_item">
            <h3 class="van-ellipsis">{{item.title}}</h3>
            <div class="img_box" v-if="item.cover.type===3">
              <van-image class="w33" fit="cover" :src="item.cover.images[0]" />
              <van-image class="w33" fit="cover" :src="item.cover.images[1]" />
              <van-image class="w33" fit="cover" :src="item.cover.images[2]" />
            </div>
            <div class="img_box" v-if="item.cover.type===1">
              <van-image class="w100" fit="cover" :src="item.cover.images[0]" />
            </div>
            <div class="info_box">
              <span>{{item.aut_name}}</span>
              <span>{{item.comm_count}}评论</span>
              <span>{{item.pubdate|relTime}}</span>
            </div>
          </div>
        </van-cell>
      </van-cell-group>
    </van-list>
  </div>
</template>

<script>
import { searchArticles } from '@/api/article'
export default {
  name: 'search-result',
  data () {
    return {
      loading: false,
      finished: false,
      // 文章列表
      articles: []
    }
  },
  created () {
    // 初始化默认=》查询条件数据
    this.query = {
      page: 1,
      q: this.$route.query.keyWord
    }
  },
  methods: {
    async onLoad () {
      //  加载数据
      const { status, data } = await searchArticles(this.query)
      if (status === 200) {
        this.articles.push(...data.results)
        // 加载完成，关闭加载效果
        this.loading = false
        // 判断数据是否全部加载完
        if (data.total_count === this.articles.length) {
          this.finished = true
        } else {
          // 页码加一
          this.query.page++
        }
      }
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  padding-top: 46px;
  height: 100%;
  overflow-y: auto;
  box-sizing: border-box;
}
.article_item {
  h3 {
    font-weight: normal;
    line-height: 2;
  }
  .img_box {
    display: flex;
    justify-content: space-between;
    .w33 {
      width: 33%;
      height: 90px;
    }
    .w100 {
      height: 180px;
      width: 100%;
    }
  }
  .info_box {
    color: #999;
    line-height: 2;
    position: relative;
    span {
      padding-right: 10px;
    }
  }
}
</style>
